package org.nyt.medicalstore.repository;

import org.nyt.medicalstore.model.DrugSimilarity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface DrugSimilarityRepository extends JpaRepository<DrugSimilarity,Long> {
    //根据两个药品ID查找
    @Query("SELECT ds FROM DrugSimilarity ds WHERE " +
            "(ds.drug1.id = :drug1Id AND ds.drug2.id = :drug2Id) OR " +
            "(ds.drug1.id = :drug2Id AND ds.drug2.id = :drug1Id)")
    Optional<DrugSimilarity> findByDrugPair(
            @Param("drug1Id") Long drug1Id,
            @Param("drug2Id") Long drug2Id);

    //根据两个药品ID删除
    @Modifying
    @Query("DELETE FROM DrugSimilarity ds WHERE " +
            "(ds.drug1.id = :drug1Id AND ds.drug2.id = :drug2Id) OR " +
            "(ds.drug1.id = :drug2Id AND ds.drug2.id = :drug1Id)")
    void deleteByDrugPair(
            @Param("drug1Id") Long drug1Id,
            @Param("drug2Id") Long drug2Id);
}
